package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * ClassName: UploadController
 * Package: com.qk.controller
 * Description:
 *
 * @Author zt
 * @Create 2025/7/30 10:27
 * @Version 1.0
 */
@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

//    public Result upload(String username, Integer age, MultipartFile file) throws  Exception{
//        log.info("{}, {}, {}", username, age, file);
//
//        //1.构造一个文件名（不能重复） --> 使用UUID
//        String fileName = file.getOriginalFilename();//获取原始文件名---> 123.1.1.png
//        String extraName = fileName.substring(fileName.lastIndexOf("."));
//        String newFileName = UUID.randomUUID().toString() + extraName;
//
//        //2.将文件保存到磁盘中---> 指定保存到哪个文件中
//        file.transferTo(new File("E:\\images\\" + newFileName));
//        return Result.success();
//    }

    /*
    文件上传
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("文件上传：{}", image);
        //构造一个文件名（不能重复） --> 使用UUID
        String fileName = image.getOriginalFilename();//获取原始文件名---> 123.1.1.png
        String extraName = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + extraName;

        String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);
    }

}
